<?php

	namespace Home\Controller;
	use THink\Controller;

	class DownloadController extends Controller {

		// private function exportCsv($filename,$data)   
		// {   
		//     header("Content-type:text/csv");   
		//     header("Content-Disposition:attachment;filename=".$filename);   
		//     header('Cache-Control:must-revalidate,post-check=0,pre-check=0');   
		//     header('Expires:0');   
		//     header('Pragma:public');   
		//     echo $data;   
		// }  
		
		// public function download() {
		// 	$result = M('search')->order('id')->select();
		// 	// var_dump($result);
		// 	$str = "id,primerid,gene,species,forwardprimer,reverseprimer,tm,productgc,productlength,describe,location,identifier,user,uploaddate\n";   
		// 	foreach ($result as $key => $row) {

		// 		$id = $row['id'];
		// 		$primerid = $row['primerid'];
		// 		$gene = $row['gene'];
		// 		$species = $row['species'];
		// 		$forwardprimer = $row['forwardprimer'];
		// 		$reverseprimer = $row['reverseprimer'];
		// 		$tm = $row['tm'];
		// 		$productgc = $row['productgc'];
		// 		$productlength = $row['productlength'];
		// 		$describe = $row['describe'];
		// 		$location = $row['location'];
		// 		$identifier = $row['identifier'];
		// 		$user = $row['user'];
		// 		$uploaddate = $row['uploaddate'];

		// 		$str .= $id.",".$primerid.",".$gene.",".$species.",".$forwardprimer.",".$reverseprimer.",".$tm.",".$productgc.",".$productlength.",".$describe.",".$location.",".$identifier.",".$user.",".$uploaddate."\n"; 
		// 	}   
		// 	$filename = 'primer_' . date('Ymd') . '.csv'; //设置文件名   
		// 	$this->exportCsv($filename, $str); //导出   
		// }


		private function exportCsv($url)   
		{   
			if (file_exists($url)) {
			    header('Content-Description: File Transfer');
			    header('Content-Type: application/octet-stream');
			    header('Content-Disposition: attachment; filename="primer_'. date('Ymd') .'.csv"');
			    header('Expires: 0');
			    header('Cache-Control: must-revalidate');
			    header('Pragma: public');
			    header('Content-Length: ' . filesize($url));
			    readfile($url);
			    exit;
			}
		}  

		public function downloadPage() {
			if (isset($_SESSION['uid'])) {
				// var_dump($_SESSION);
				$_SESSION['url'] = $_SERVER['PHP_SELF'];
				$whichTag = 4;
				$condition = 1;
				$this->assign('username', $_SESSION['username']);
			}
			else {
				$whichTag = 3;
				$condition = 0;
			}
			$this->assign('condition', $condition);
			$this->assign('whichTag', $whichTag);
			$this->display();
		}


		public function download() {
			$mysqli = new \mysqli("localhost", "root", "", "sysublast");

			if ($mysqli->connect_errno) {
			    printf("Connect failed: %s\n", $mysqli->connect_error);
			    exit();
			}

			$dir = '../' . __ROOT__ . '/Public/data/' . date('Ymd') . '/';
			if (!is_dir($dir)) {
				mkdir($dir);
			}
			$filename = date('Ymd') . '/primer_' . mt_rand() . '.csv'; //设置文件名
			// var_dump($filename);

			$result = $mysqli->query("select * from ( select 'id','primerid','gene','species','forwardprimer','reverseprimer','tm','productgc','productlength','describe','location','identifier','user','uploaddate' union all select * from blast_search) a into outfile 'D:/wamp64/www/primer/Public/data/" . $filename . "' fields terminated by ',' optionally enclosed by '\"' lines terminated by '\n';");

			// var_dump("select * from ( select 'id','primerid','gene','species','forwardprimer','reverseprimer','tm','productgc','productlength','describe','location','identifier','user','uploaddate' union all select * from blast_search) a into outfile 'D:/wamp64/www/primer/Public/data/" . $filename . "' fields terminated by ',' optionally enclosed by '\"' lines terminated by '\n';");
			if($result) {
				$url = '..' .  __ROOT__ . '/Public/data/' . $filename;
				$this->exportCsv($url);
			}
		}
	}
?>